﻿using Google.GData.Client;
using System;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace Desktop.Library
{
    [Serializable()]
    public class GooglePreferences : ISerializable, INotifyPropertyChanged
    {
        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Variables

        private string _username;
        private string _password;
        private GDataCredentials _credentials;

        #endregion

        #region Constructors

        public GooglePreferences()
        {
            Username = string.Empty;
            Password = string.Empty;
        }

        public GooglePreferences(SerializationInfo info, StreamingContext context)
        {
            Username = info.GetString("Username");
            Password = info.GetString("Password");
        }

        #endregion

        #region Properties

        public string Username
        {
            get
            {
                return _username;
            }
            set
            {
                if (_username != value)
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Username"));
                    }
                }
                _username = value;
            }
        }

        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                if (_password != value)
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Password"));
                    }
                }
                _password = value;
            }
        }

        public GDataCredentials Credentials
        {
            get
            {
                try
                {
                    if (_credentials == null)
                    {
                        _credentials = new GDataCredentials(Username, Password);
                    }
                }
                catch (Exception ex)
                {
                    _credentials = null;
                    throw ex;
                }
                return _credentials;
            }
        }

        #endregion

        #region Methods

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Username", Username);
            info.AddValue("Password", Password);
        }

        #endregion
    }
}